﻿using System.Linq;
using Fuse8.ContentManagementFramework.Domain;
using Fuse8.ContentManagementFramework.Domain.Commands;
using Fuse8.DomainFramework.Common.Locator;
using Fuse8.DomainFramework.DataAccess;
using Fuse8.DomainFramework.DataAccess.SqlDatabase;

namespace Fuse8.ContentManagementFramework.DataAccess.SqlAccess.Commands
{
    internal class CmsPageControlsChangeTemplateRegionsDataCommand : CmsPageControlsChangeTemplateRegionsCommand
    {
        public override IQueryable<CmsPageControl> Execute()
        {
            var factory = DependencyServiceLocator.Current.GetInstance<IUnitOfWorkFactory<CmsPageControl>>();

            var currentUnitOfWork = factory.Get(false) as SqlUnitOfWork<CmsPageControl>;

            if (currentUnitOfWork != null)
            {
                var mapper = DependencyServiceLocator.Current.GetInstance<ISqlEntityDataMapper<CmsPageControl, int>>();

                SqlUtility<CmsPageControl, int>.ExecuteNonQuery(
                        currentUnitOfWork.Connection,
                        currentUnitOfWork.Transaction,
                        "cms_sp_PageControlChangeTemplateRegions",
                        new
                        {
                            PreviousPageId = CommandParameter.PreviousPageId,
                            LatestPageId = CommandParameter.LatestPageId
                        },
                        mapper);
            }

            return null;
        }
    }
}